﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CenterDateAchieve.Forms;
using CenterDateAchieve.Helper;
using BLL.Business;
using AppToolKit.Core.Context.Spring;
using Common.Logging;
using Utility;
using IBLL.Controller;
using BLL.Controller;
using CenterDateAchieve.Controller;

namespace CenterDateAchieve
{
    public partial class TaskDistributeForm : FormBase
    {
        public TaskDistributeForm()
        {
            InitializeComponent();
        }
        [AutoWiring]
        public ControllerFactory ControllerFactoryHolder { get; set; }
        [AutoWiring]
        public TaskFaced TaskFacedHolder { get; set; }
        public AllotMentBuesinessDao AllotMentBuesinessDaoHolder { get; set; }
        private const string Code = "code";
        ILog log = LogManager.GetLogger(LogDefault.AppInfo);

        private void TaskDistributeForm_Load(object sender, EventArgs e)
        {
            try
            {
                BulidTaskList();
                RenderDataGridView(string.Empty);
            }
            catch (Exception ex)
            {
                log.Debug(ex);
            }
        }
        void BulidTaskList()
        {
            TreeViewHelper.BulidTreeView(this.treeTasklist, "任务", TaskFacedHolder.GetTaskListAll(string.Empty), null);
            treeTasklist.ExpandAll();
        }

        void RenderDataGridView(string taskList)
        {
           this.GridTask.DataSource=TaskFacedHolder.GetTaskAll(taskList).Tables[0];
        }
        void RenderListView(string taskList)
        {
            this.ListTask.DataSource = TaskFacedHolder.GetTaskAllot(taskList).Tables[0];
            ListTask.DisplayMember = "descript";
            ListTask.ValueMember = "code";

        }
         private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         TreeNode CurrentNode_;
         private void treeTasklist_AfterSelect(object sender, TreeViewEventArgs e)
         {
             if (CurrentNode_ != null)
                 CurrentNode_.ForeColor = Color.Black;
             log.Debug("e.node.name:" + e.Node.Name);
             if (e.Node.Level == 1)
             {
                 Refresh(e.Node.Name);
                
                 CurrentNode_ = e.Node;

                 e.Node.ForeColor = Color.Red;
             }
             else
             {
                
                 CurrentNode_ = null;
             }
         }
         private void Refresh(string taskList)
         {
             RenderListView(taskList);
             RenderDataGridView(taskList);
         }
         private void btAdd_Click(object sender, EventArgs e)
         {
             List<string> listID = new List<string>();
             foreach (DataGridViewRow dr in GridTask.SelectedRows)
             {
                 listID.Add(dr.Cells[Code].Value.ToString());

             }
             if (listID.Count != 0 && CurrentNode_ != null)
             {
                 TaskFacedHolder.AddAllot(CurrentNode_.Name, listID);

                 Refresh(CurrentNode_.Name);
             }
         }

        

         private void btAddAll_Click(object sender, EventArgs e)
         {
             List<string> listID = new List<string>();
             foreach (DataGridViewRow dr in GridTask.Rows)
             {
                 listID.Add(dr.Cells[Code].Value.ToString());

             }
             if (listID.Count != 0 && CurrentNode_ != null)
             {
                 TaskFacedHolder.AddAllot(CurrentNode_.Name, listID);

                 Refresh(CurrentNode_.Name);
             }
         }

         private void btDel_Click(object sender, EventArgs e)
         {
             if (CurrentNode_ == null||ListTask.Items.Count==0)
                 return;
             string aa = string.Empty;
             string bb=string.Empty ;
             aa = ListTask.SelectedValue.ToString ();
             bb = CurrentNode_.Name;
             TaskFacedHolder.DelTaskAllot(bb, aa);
             Refresh(CurrentNode_.Name);
         }

         private void btDelAll_Click(object sender, EventArgs e)
         {
             if (CurrentNode_ == null)
                 return;
             string bb = string.Empty;
             bb = CurrentNode_.Name;
             TaskFacedHolder.DelTaskAllot(bb, "");
             Refresh(CurrentNode_.Name);
         }

         private void btNewTaskList_Click(object sender, EventArgs e)
         {
             IFormController controller = ControllerFactoryHolder.GetController<TaskListController, ListDefineForm>();

             controller.Show();
         }

         private void treeTasklist_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
         {
             if (e.Node.Level == 1)
             {
                 IFormController controller = ControllerFactoryHolder.GetController<TaskListController, ListDefineForm>(e.Node.Name);

                 controller.Show();
             }
         }

         private void btNewTask_Click(object sender, EventArgs e)
         {
             IFormController controller = ControllerFactoryHolder.GetController<TaskController, TaskDefineForm>();

             controller.Show();
         }

         private void GridTask_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
         {
             if (e.RowIndex == -1)
                 return;
             IFormController controller = ControllerFactoryHolder.GetController<TaskController, TaskDefineForm>(GridTask.Rows[e.RowIndex].Cells["code"].Value);

             controller.Show();
         }
    }
}
